package com.zcx.merchantorder.http.entity;

import com.zcx.merchantorder.base.BaseApplication;
import com.zcx.merchantorder.util.SDCardUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

import lombok.Data;

/**
 * @Description: 测试的用户实体类
 * @Author: zcx
 * @CreateDate: 2019/3/3 10:54
 */
@Data
public class User implements Serializable,Cloneable{

    public final static String TAG = "LoginUserBean";
    public final static String BASE_CACHE_PATH = BaseApplication.getSingleApplication().getFilesDir().getParent() + "/";//缓存的路径;;
    private static User instance;

    public static User getInstance() {
        if (instance == null) {
            Object object = SDCardUtils.restoreObject(BASE_CACHE_PATH + TAG);
            if (object == null) { // App第一次启动，文件不存在，则新建之
                object = new User();
                SDCardUtils.saveObject(BASE_CACHE_PATH + TAG, object);

            }
            instance = (User) object;
        }
        return instance;
    }

    private String name;
    private String age;
    private String access_token;

    public void save() {
        instance = this;
        SDCardUtils.saveObject(BASE_CACHE_PATH + TAG, this);
    }

    // -----------以下3个方法用于序列化-----------------
    public User readResolve() throws ObjectStreamException, CloneNotSupportedException {
        instance = (User) this.clone();
        return instance;
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        ois.defaultReadObject();
    }

    public Object Clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
